﻿// -------------------------------------------------------------------------
//    @FileName         :    NFPartnerPart.cpp
//    @Author           :    gaoyi
//    @Date             :    24-8-30
//    @Email            :    445267987@qq.com
//    @Module           :    NFPartnerPart
//
// -------------------------------------------------------------------------

#include "NFPartnerPart.h"

#include "proto_city.pb.h"
#include "proto_cs_login.pb.h"
#include "proto_db.pb.h"

NFPartnerPart::NFPartnerPart()
{
    if (EN_OBJ_MODE_INIT == NFShmMgr::Instance()->GetCreateMode())
    {
        CreateInit();
    }
    else
    {
        ResumeInit();
    }
}

NFPartnerPart::~NFPartnerPart()
{
}

int NFPartnerPart::CreateInit()
{
    return 0;
}

int NFPartnerPart::ResumeInit()
{
    return 0;
}

int NFPartnerPart::LoadFromDB(const proto_ff::tbRoleDetail* pDBData)
{
    m_stData.FromPb(pDBData->role_base_data().partner_data());
    return 0;
}

int NFPartnerPart::InitConfig(proto_ff::tbRoleDetail* pDbData, const RoleOne& stRoleOne)
{
    auto pRolePartnerData = pDbData->mutable_role_base_data()->mutable_partner_data();
    CHECK_NULL(0, pRolePartnerData);

    pRolePartnerData->clear_role_partner_list();

    for (int i = 0; i < 3; ++i)
    {
        pRolePartnerData->add_battle_partner_id(0);
    }

    pRolePartnerData->set_follow_partner_id(0);

    pRolePartnerData->set_last_op_time(NF_ADJUST_TIMENOW());

    pRolePartnerData->set_ai_type(proto_ff::EN_PARTNER_AI_ATTACK);

    for (int i = 0; i < 3; ++i)
    {
        auto pPartnerData = pRolePartnerData->add_battle_partner_data();
        if (pPartnerData)
        {
            for (int j = 0; j < 3; ++j)
            {
                auto pEquipData = pPartnerData->add_role_partner_equip_list();
                if (pEquipData)
                {
                    for (int k = 0; k < 4; ++k)
                    {
                        pEquipData->add_stone_list();
                    }
                }
            }
        }
    }

    return 0;
}

int NFPartnerPart::SaveDB(proto_ff::tbRoleDetail* pDBData)
{
    m_stData.ToPb(pDBData->mutable_role_base_data()->mutable_partner_data());
    return 0;
}

int NFPartnerPart::MakePlayerDataCS(proto_ff::Proto_CSGetRoleRsp* pCSGetRoleRsp)
{
    m_stData.ToPb(pCSGetRoleRsp->mutable_partner_data());
    return 0;
}

int NFPartnerPart::MakeCityPlayerData(proto_ff::CityRoleInfo* pGCCityRoleInfo)
{
    pGCCityRoleInfo->set_follow_partner_id(m_stData.follow_partner_id);
    PartnerOne* pPartnerOne = FindPartner(m_stData.follow_partner_id);
    if (pPartnerOne)
    {
        pGCCityRoleInfo->set_follow_partner_fatigue(pPartnerOne->is_fatigue);
        pGCCityRoleInfo->set_follow_partner_star(pPartnerOne->star);
    }
    return 0;
}

PartnerOne* NFPartnerPart::FindPartner(int iID)
{
    auto iter = m_stData.role_partner_list.find(iID);
    if (iter != m_stData.role_partner_list.end())
    {
        return &iter->second;
    }

    return nullptr;
}

PartnerOne* NFPartnerPart::GetBattlePartner(proto_ff::EPartnerType eType)
{
    int iIndex = (int)eType - 1;
    int iPartnerID = m_stData.battle_partner_id[iIndex];

    if (iPartnerID > 0)
    {
        return FindPartner(iPartnerID);
    }

    return nullptr;
}
